import {
    createEntityBase,
    loadAllBase,
    loadEntityBase,
    pageListBase,
    removeEntityBase,
    updateEntityBase
} from "../../../components/EntityApiBase.js";
import axios from "axios";
import {ElMessage} from "element-plus";

const basePath = import.meta.env.VITE_BACKEND_BASE_URL;

const entityPath = "/api/app";

async function loadEntity(id) {
    return await loadEntityBase(entityPath, id);
}

async function loadAll() {
    return await loadAllBase(entityPath);
}

async function pageList(paginationDTO) {
    return await pageListBase(entityPath, paginationDTO);
}

async function createEntity(entity) {
    return await createEntityBase(entityPath, entity);
}

async function updateEntity(id, entity) {
    return await updateEntityBase(entityPath, id, entity);
}

async function removeEntity(id) {
    return await removeEntityBase(entityPath, id);
}

async function appApiRel(appId, apiIdList) {
    try {
        let {data} = await axios.post(basePath + entityPath + "/apiRel/" + appId, apiIdList);
        if (data.code === '1') {
            return data.data;
        } else {
            ElMessage.error(data.msg)
        }
    } catch (error) {
        console.error(error);
    }
}

async function getApiByAppId(appId) {
    try {
        let {data} = await axios(basePath + entityPath + "/apiRel/" + appId);
        if (data.code === '1') {
            return data.data;
        } else {
            ElMessage.error(data.msg)
        }
    } catch (error) {
        console.error(error);
    }
}

export {loadEntity, loadAll, pageList, createEntity, updateEntity, removeEntity, appApiRel, getApiByAppId};

